package View;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Controller.ContactController;
import Controller.FormActionListener;
import Model.Contact;
import Model.Group;

/**
 * view of a form to create or modify a contact
 * @author Vincent Francolin	
 * @version 1.0 : creation
 */
public class FormView extends JPanel {
	private JTextField name;
	private JTextField firstName;
	private JTextField companyAddress;
	private JTextField mobilePhone;
	private JTextField homePhone;
	private JTextField mobilePhonePro;
	private JTextField officePhone;
	private JTextField homePage;
	private JTextField mailPro;
	private JTextField mailPerso;
	private String picPath;
	private JTextField other;
	
	private JButton changePhoto;
	private JButton accept;
	
	private JComboBox group;
	
	/**
	 * default constructor
	 * @param c contact data in case of modification
	 * @param controller the main controller of the application
	 */
	public FormView(Contact c,Group g,ContactController controller) {
		super();
		group = new JComboBox(controller.getGroupList());
		if(c != null){
			this.name = new JTextField(c.getName());
			this.firstName = new JTextField(c.getFirstName());
			this.companyAddress = new JTextField(c.getCompanyAddress());
			this.mobilePhone = new JTextField(c.getMobilePhone());
			this.homePhone = new JTextField(c.getHomePhone());
			this.mobilePhonePro = new JTextField(c.getMobilePhonePro());
			this.officePhone = new JTextField(c.getOfficePhone());
			this.homePage = new JTextField(c.getHomePage());
			this.mailPro = new JTextField(c.getMailPro());
			this.mailPerso = new JTextField(c.getMailPerso());
			this.picPath = c.getPicPath();
			this.other = new JTextField(c.getOther());
			if(g!=null)
				group.setSelectedItem(g.getName());
			else
				group.setSelectedIndex(0);
		} else {
			this.name = new JTextField();
			this.firstName = new JTextField();
			this.companyAddress = new JTextField();
			this.mobilePhone = new JTextField();
			this.homePhone = new JTextField();
			this.mobilePhonePro = new JTextField();
			this.officePhone = new JTextField();
			this.homePage = new JTextField();
			this.mailPro = new JTextField();
			this.mailPerso = new JTextField();
			this.picPath = "default.png";
			this.other = new JTextField();
		}
		Dimension dim = new Dimension (100,20);
		//Dimension dim2 = new Dimension (200,20);
		this.name.setPreferredSize(dim);
		this.firstName.setPreferredSize(dim);
		this.companyAddress.setPreferredSize(dim);
		this.mobilePhone.setPreferredSize(dim);
		this.homePhone.setPreferredSize(dim);
		this.mobilePhonePro.setPreferredSize(dim);
		this.officePhone.setPreferredSize(dim);
		this.homePage.setPreferredSize(dim);
		this.mailPro.setPreferredSize(dim);
		this.mailPerso.setPreferredSize(dim);
		this.other.setPreferredSize(dim);
		
		accept = new JButton("Save");
		changePhoto = new JButton("Change picture");
		
		accept.addActionListener(new FormActionListener(this,controller,c));
		changePhoto.addActionListener(new FormActionListener(this,controller,c));
		
		initView();
	}
	
	/**
	 * initialize the view of the form
	 */
	private void initView(){
        this.setLayout(new GridBagLayout());
        GridBagConstraints cons = new GridBagConstraints();
        
        cons.gridy = 0;
        cons.insets = new Insets(5,5,5,10); 
        cons.gridx = 0;
        add(changePhoto,cons);  
        
        cons.gridy++;
        add(new JLabel("Name : "),cons);      
        cons.gridx = 1;
        add(name,cons);       
        cons.gridx = 2;
        add(new JLabel("First name : "),cons);     
        cons.gridx = 3;
        add(firstName,cons);
        
        cons.insets = new Insets(5,5,1,5); 
        cons.gridy++;
        cons.gridx = 0;
        add(new JLabel("Office : "),cons);
        
        cons.gridy++;
        add(new JLabel("Company address : "),cons);   
        cons.gridx = 1;
        add(companyAddress,cons);
        
        cons.gridy++;
        cons.gridx = 0;
        cons.gridwidth = 1;
        add(new JLabel("Office phone : "),cons);
        cons.gridx = 1;
        add(officePhone,cons);
        cons.gridx = 2;
        add(new JLabel("Professional mobile : "),cons);
        cons.gridx = 3;
        add(mobilePhonePro,cons);
        
        cons.insets = new Insets(5,5,5,5);
        cons.gridy++;
        cons.gridx=0;
        add(new JLabel("Office mail : "),cons);
        cons.gridx = 1;
        add(mailPro,cons);
        
        cons.gridy++;
        cons.gridx = 0;
        add(new JLabel("Personnal : "),cons);
        
        cons.insets = new Insets(5,5,1,5);
        cons.gridy++;
        cons.gridx = 0;
        add(new JLabel("Home phone : "),cons);
        cons.gridx = 1;
        add(homePhone,cons);
        cons.gridx = 2;
        add(new JLabel("Personnal Mobile : "),cons);
        cons.gridx = 3;
        add(mobilePhone,cons);
        
        cons.gridy++;
        cons.gridx = 0;
        add(new JLabel("Personnal mail : "),cons);
        cons.gridx = 1;
        add(mailPerso,cons);
        
        cons.gridy++;
        cons.gridx = 0;
        add(new JLabel("Website : "),cons);
        cons.gridx = 1;
        add(homePage,cons);
        
        cons.gridy++;
        cons.gridx = 0;
        add(new JLabel("Group : "),cons);
        cons.gridx = 1;
        add(group,cons);
        
        cons.gridy++;
        cons.gridx = 0;
        add(accept,cons);
	}
	
	public void setPicPath(String picPath){
		this.picPath = picPath;
	}
	
	/**
	 * Create a contact from the data in the text fields 
	 * @return a newly created contact or null if some field are empty
	 */
	public Contact getContactFromEntries(){
		String nameStr = name.getText();
		String firstNameStr = firstName.getText();
		String officeAddressStr = companyAddress.getText();
		String officePhoneStr = officePhone.getText();
		String officeMobileStr = mobilePhonePro.getText();
		String officeMailStr = mailPro.getText();
		String persoPhoneStr = homePhone.getText();
		String persoMobileStr = mobilePhone.getText();
		String persoMailStr = mailPerso.getText();
		String websiteStr = homePage.getText();
		
		if(nameStr.equals("") || firstNameStr.equals("") || officeAddressStr.equals("") ||
				officePhoneStr.equals("") || officeMobileStr.equals("") || officeMailStr.equals("") ||
				persoPhoneStr.equals("") || persoMobileStr.equals("") || persoMailStr.equals("") ||
				websiteStr.equals(""))
			return null;
		
		name.setText("");
		firstName.setText("");
		companyAddress.setText("");
		officePhone.setText("");
		mobilePhonePro.setText("");
		mailPro.setText("");
		homePhone.setText("");
		mobilePhone.setText("");
		mailPerso.setText("");
		homePage.setText("");
		
		return new Contact(nameStr,firstNameStr,officeAddressStr,persoMobileStr,persoPhoneStr,officeMobileStr,
				officePhoneStr,websiteStr,officeMailStr,
				persoMailStr,picPath,"");
	}
	
	/**
	 * retrieve the group from the combo box
	 * @return a newly created group based on the selected value
	 */
	public Group getGroupFromEntries(){
		String val = (String)group.getSelectedItem();
		if(val.equals("none"))
			return null;
		return new Group(val);
	}
	
}
